
package com.probiz.estore.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.core.event.EventUtil;
import com.probiz.estore.core.event.FrontCacheClearEvent;
import com.probiz.estore.core.util.ContextUtil;

public class FrontCacheRefreshInterceptor implements MethodInterceptor {
	protected final transient Log	logger	= LogFactory.getLog(getClass());

	/**
	 * Return a description for the given method invocation.
	 * 
	 * @param invocation
	 *            the invocation to describe
	 * @return the description
	 */
	protected String getInvocationDescription(MethodInvocation invocation) {
		return "method '" + invocation.getMethod().getName() + "' of class ["
				+ invocation.getThis().getClass().getName() + "]";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
	 */
	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		Object returnObj = null;
		try {
			returnObj = methodInvocation.proceed();
			if(ConfigUtil.getInstance().getServerSidePageCacheSeconds() > 0 && !ContextUtil.getInstance().isFront()){
				//清理前台cache
				EventUtil.getInstance().fire(new FrontCacheClearEvent(this));
			}
		} catch (Throwable e) {
			e.printStackTrace();
			throw e; //不能删除throw e
		} finally {
		}
		return returnObj;
	}
}
